package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntKeyInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntKeyInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntKeyInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntKeyInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.MchntKeyInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.SmsTypeEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SmsService;
import cc.rengu.oltp.service.realize.impl.SmsServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 商户密钥信息查询
 * Created by 王成 on 2020/3/25
 */
public class MchntKeyInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntKeyInfoQueryRequest mchntKeyInfoQueryRequest = new MchntKeyInfoQueryRequest();
        ConvertUtil.convertOutput(mchntKeyInfoQueryRequest);
        return mchntKeyInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntKeyInfoQueryRequest mchntKeyInfoQueryRequest = (MchntKeyInfoQueryRequest) request;
        MchntKeyInfoQueryResponse mchntKeyInfoQueryResponse = new MchntKeyInfoQueryResponse();
        BeanUtil.beanCopy(mchntKeyInfoQueryRequest, mchntKeyInfoQueryResponse);
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(mchntKeyInfoQueryRequest.getUserId()) || StringUtil.isEmptyOrNull(mchntKeyInfoQueryRequest.getMchntNo()) ||
                StringUtil.isEmptyOrNull(mchntKeyInfoQueryRequest.getKeyType()) || StringUtil.isEmptyOrNull(mchntKeyInfoQueryRequest.getSmsCode()) || StringUtil.isEmptyOrNull(mchntKeyInfoQueryRequest.getSmsAuthId())) {
            rglog.error("用户编号<{}>,商户号<{}>,密钥类型<{}>,短信验证码<{}>,短信验证码唯一标识<{}>不能为空!",
                    mchntKeyInfoQueryRequest.getUserId(), mchntKeyInfoQueryRequest.getMchntNo(), mchntKeyInfoQueryRequest.getKeyType(), mchntKeyInfoQueryRequest.getSmsCode(), mchntKeyInfoQueryRequest.getSmsAuthId());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 校验短信验证码 */
        SmsService smsService = new SmsServiceImpl();
        if (!smsService.verifySmsCode(mchntKeyInfoQueryRequest.getHeader().getInstId(), SmsTypeEnum.IDENTITY_AUTH.getSmsType(), mchntKeyInfoQueryRequest.getSmsCode(), mchntKeyInfoQueryRequest.getSmsAuthId())) {
            rglog.error("短信验证码失败!");
            throw new BizException(RespCodeEnum.VERIFY_SMS_CODE_ERROR.getRespCode(), RespCodeEnum.VERIFY_SMS_CODE_ERROR.getRespDesc());
        }
        /* 查询商户密钥信息 */
        BspsMchntKeyInfoMapper bspsMchntKeyInfoMapper = new BspsMchntKeyInfoMapperImpl();
        String mchntAppId = StringUtil.isEmptyOrNull(mchntKeyInfoQueryRequest.getMchntAppId()) ? "APPID" : mchntKeyInfoQueryRequest.getMchntAppId();
        MchntKeyInfo mchntKeyInfo = bspsMchntKeyInfoMapper.selectBspsMchntKeyInfoByPrimaryKey(mchntKeyInfoQueryRequest.getHeader().getInstId(), mchntKeyInfoQueryRequest.getMchntNo(), mchntAppId, mchntKeyInfoQueryRequest.getKeyType());
        if (null != mchntKeyInfo) {
            mchntKeyInfoQueryResponse.setAlgorithmType(mchntKeyInfo.getAlgorithmType());
            mchntKeyInfoQueryResponse.setKeyValue(mchntKeyInfo.getKeyValue());
        }
        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntKeyInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntKeyInfoQueryResponse mchntKeyInfoQueryResponse = (MchntKeyInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntKeyInfoQueryResponse);
    }
}
